<template>
  <el-dialog v-model="dialogVisible" title="导师打分" width="500">
    <el-form
      :model="scoreForm"
      ref="scoreFormRef"
      label-width="auto"
      style="width: 300px"
      v-loading="loading"
    >
      <el-form-item label="第一阶段答辩得分">
        <el-input-number
          v-model="scoreForm.dbFirst"
          :min="0"
          :step="1"
          :max="100"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="第二阶段答辩得分">
        <el-input-number
          v-model="scoreForm.dbSecond"
          :min="0"
          :step="1"
          :max="100"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="中期答辩得分">
        <el-input-number
          v-model="scoreForm.dbMid"
          :min="0"
          :step="1"
          :max="100"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="终期答辩得分">
        <el-input-number
          v-model="scoreForm.dbTerminal"
          :min="0"
          :step="1"
          :max="100"
          style="width: 150px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          :loading="loading"
          type="primary"
          @click="onAppend(scoreFormRef)"
        >
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import api from "@/api";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refresh"]);

const dialogVisible = ref<boolean>(false);

const loading = ref<boolean>(false);
const scoreFormRef = ref();
const scoreForm = ref<any>({
  dbFirst: "",
  dbSecond: "",
  dbMid: "",
  dbTerminal: "",
});

watch(dialogVisible, (bool) => {
  if (!bool) {
    scoreForm.value = {
      dbFirst: "",
      dbSecond: "",
      dbMid: "",
      dbTerminal: "",
    };
  }
});

function onAppend(formEl: FormInstance | undefined) {
  if (!formEl) return;
  //@ts-ignore
  formEl.validate((valid) => {
    if (valid) {
      loading.value = true;
      api.group
        .updateTeam(scoreForm.value)
        .then((res: any) => {
          ElMessage({
            message: res.message,
            type: res.success ? "success" : "error",
            grouping: true,
            showClose: true,
          });
          if (res.success) {
            emit("refresh");
            dialogVisible.value = false;
            loading.value = false;
          }
        })
        .catch(() => {
          loading.value = false;
        });
    }
  });
}

const open = (row: any) => {
  if (row) {
    scoreForm.value = {
      id: row?.id,
      dbFirst: row?.dbFirst,
      dbSecond: row?.dbSecond,
      dbMid: row?.dbMid,
      dbTerminal: row?.dbTerminal,
    };
  }

  dialogVisible.value = true;
};

defineExpose({ open });
</script>
